<?php
declare(strict_types=1);
namespace Service\UserService;

use Service\BaseService;
use App\Model\PssModel\Business;
class ChannelUserService extends BaseService
{
    /**
     * 获取用户信息
     * @param number $bid
     */
    public function getUserInfoByBid( $bid = 0 )
    {
        $info = Business::instance()
        ->select([
            'id',
            'id as bid',
            'name',
            'appid',
            'secret',
            'open_url',
            'channel_id',
            'is_channel',
            'order_declare',
            'user_source',
            'user_level',
            'user_type',
            'user_tag',
            'total_balance_settlement_money',
            'settlement_warn_money',
            'settlement',
            'is_market_price_send',
            'template',
            'sync_contents',
            'sync_mode',
            'sync_status',
            'pay_declare',
            'is_stock',
            'order_declare',
            'is_skip_declare',
            'is_skip_settlement',
            'is_router_user',
            'is_bbc_self_take',
            'yx_template_id',
            'hz_template_id',
            'account',
            'status',
            'is_support_aftersale_verify'
        ])
        ->where('id',$bid)
        ->first();
        if( empty($info) ) {
            return [];
        }
    
        return $info->toArray();
    }
    
    /**
     * 获取用户明细
     * @param int $page
     * @param int $limit
     * @return mixed
     */
    public function getUserList(int $page,int $limit = 100)
    {
        $columns = [
            'id',
            'id as bid',
            'name',
            'is_market_price_send',
            'user_source',
            'user_tag',
            'is_mult_shop',
            'open_url',
            'is_sync',
            'sync_status',
            'template',
            'has_template',
            'template_price_tpl',
            'is_goods_expire',
            'is_stock_limit',
            'is_goods_down',
            'is_push_goods',
            'is_router_user',
            'user_tax_mode'
        ];
        
        return Business::instance()
        ->where('status',0)
        ->where('user_source',2)
        ->where('has_template',1)
        ->where('open_url','<>','')
        ->where('template_price_tpl','<>','')
        ->orderBy('id','asc')
        ->paginate($limit,$columns,'page', $page,true)
        ->toArray();
    }
    
    /**
     * 获取所有满足需要推送商品信息变化的渠道
     * @param int $page
     * @param int $limit
     * @return mixed
     */
    public function getAllPushChannelUsers()
    {
        $columns = [
            'id',
            'id as bid',
            'name',
            'is_market_price_send',
            'user_source',
            'user_tag',
            'is_mult_shop',
            'open_url',
            'is_sync',
            'sync_status',
            'template',
            'has_template',
            'template_price_tpl',
            'is_goods_expire',
            'is_stock_limit',
            'is_goods_down',
            'is_push_goods',
            'is_router_user',
            'user_tax_mode',
            'is_channel',
            'channel_id',
            'is_full_support_after_sale'
        ];
    
        return Business::instance()
        ->select($columns)
        ->where('status',0)
        ->where('user_source',2)
        ->where('has_template',1)
        ->where('open_url','<>','')
        ->where('template_price_tpl','<>','')
        ->orderBy('id','asc')
        ->get()
        ->toArray();
    }
}